package com.windystory.wslibrary.ui.line;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.windystory.wslibrary.R;

/**
 * 虚线
 */
public class DashLineView extends View {

    private Paint mPaint;
    private Path mPath;
    private int dashlineSize;
    private int dashlineColor;
    private String colorDefault = "#E2E0E0";
    private static final int UNSPECIFIED_INT = Integer.MAX_VALUE;

    public DashLineView(Context context) {
        super(context);
    }

    public DashLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
    }


    private void getAttrs(Context context, AttributeSet attrs) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPath = new Path();
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DashLine);
            dashlineSize = a.getInt(R.styleable.DashLine_lineSize, 10);
            dashlineColor = a.getColor(R.styleable.DashLine_lineColor, UNSPECIFIED_INT);
            if (dashlineColor == UNSPECIFIED_INT)
                mPaint.setColor(Color.parseColor(colorDefault));
            else
                mPaint.setColor(dashlineColor);


        }
        mPaint.setStrokeWidth(3);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setPathEffect(new DashPathEffect(new float[]{15, dashlineSize}, 0));

    }


    @Override
    protected void onDraw(Canvas canvas) {
        int centerY = getHeight() / 2;
        mPath.reset();
        mPath.moveTo(0, centerY);
        mPath.lineTo(getWidth(), centerY);
        canvas.drawPath(mPath, mPaint);
    }
}